package com.warmlight.voicepacket;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smp.soundtouchandroid.SoundStreamAduioRecorder;
import com.smp.soundtouchandroid.SoundTouch;
import com.umeng.analytics.MobclickAgent;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;
import com.warmlight.voicepacket.base.BaseActivity;
import com.warmlight.voicepacket.data.RecommendFeed;
import com.warmlight.voicepacket.data.eventbus.TeaseHideFloat;
import com.warmlight.voicepacket.data.eventbus.TeaseRelease;
import com.warmlight.voicepacket.ui.Add2NetListActivity;
import com.warmlight.voicepacket.utils.FileUtils;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.SPUtils;
import com.warmlight.voicepacket.utils.TimeFormatUtils;
import com.warmlight.voicepacket.utils.WaveHeader;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;

public class RecordActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_time;
    private IndicatorSeekBar seekbar_yindiao;
    private TextView tv_yindiao_clear;
    private IndicatorSeekBar seekbar_speed;
    private TextView tv_speed_clear;
    private ImageView iv_record_start;
    private ImageView iv_record_cancel;
    private ImageView iv_record_ok;
    private SoundTouch soundTouch;
    private SoundStreamAduioRecorder soundTouchRec;
    private ImageView iv_record_stop;
    private ImageView iv_record_play;
    private ImageView iv_record_pause;

    private String lastRecordFile;
    private boolean isPlaying = false;
    private boolean finishEncodeDecode = false;
    private boolean needEncodeDecode = false;
    private CheckBox cb_public;
    private LinearLayout ll_public;
    private String type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_record);
        if (!SPUtils.isLogined()){
            showToast("登录后才能使用");
            finish();
        }else {
            //判断是否是从好听就撩 过来的，如果是，就在发布成功后返回好听就撩  type = tease
            Bundle bundle = getIntent().getExtras();
            if (bundle != null){
                if (bundle.containsKey("type")){
                    type = bundle.getString("type");
                }
            }
            initView();
            initData();
            initListener();
//            if (!TextUtils.equals("tease",type)){
            EventBus.getDefault().post(new TeaseHideFloat(false));
//            }
            showDialog();
        }
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void teaseRelease(TeaseRelease teaseRelease){
        if (!TextUtils.isEmpty(type) && TextUtils.equals("tease",type)){
            finish();
        }
    }

    private void showDialog() {
        boolean aBoolean = SPUtils.getBoolean(SPUtils.VOICECHANGE);
        if (!aBoolean){
            AlertDialog alertDialog = new AlertDialog.Builder(getContext())
                    .setTitle("功能使用条款")
                    .setMessage("1. 在用户遵守用户使用条款及隐私策略（可在[我的]界面下查看）的前提下，本产品授予用户关于本产品有限的、不可转让的、非独占的、可撤销的、仅为个人  用途的非商业性的使用的权利。\n" +
                            " 2. 为保证传播内容的合法性，使用本产品生产制作的内容会上传审核，若发现违规内容则此条内容将不可使用\n" +
                            " 3. 玩皮语音保留本使用条款未特别授予的所有权利")
                    .setCancelable(false)
                    .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //埋点
                            MobclickAgent.onEvent(getContext(), MobClickHelper.RECORD_TERM_yes);
//                HashMap<String, Object> temp = new HashMap<>();
//                temp.put("登录返回", "");
                            ZhugeSDK.getInstance().track(getContext(), MobClickHelper.RECORD_TERM_yes);
                            dialog.dismiss();
                            SPUtils.putBoolean(SPUtils.VOICECHANGE,true);
                        }
                    }).create();

            alertDialog.show();

        }
    }


    private void initData(){
        iv_record_ok.setVisibility(View.GONE);
        iv_record_cancel.setVisibility(View.GONE);
        iv_record_pause.setVisibility(View.GONE);
        iv_record_play.setVisibility(View.GONE);
        iv_record_stop.setVisibility(View.GONE);
        seekbar_speed.setIndicatorTextFormat("${PROGRESS} %");
        soundTouch = new SoundTouch(0, 2, 1, 2, 1, 1);
        soundTouchRec = new SoundStreamAduioRecorder(this, soundTouch);
    }

    private void initListener() {
        iv_record_start.setOnClickListener(this);
        iv_record_cancel.setOnClickListener(this);
        iv_record_ok.setOnClickListener(this);
        tv_speed_clear.setOnClickListener(this);
        tv_yindiao_clear.setOnClickListener(this);
        iv_record_stop.setOnClickListener(this);
        iv_record_pause.setOnClickListener(this);
        iv_record_play.setOnClickListener(this);
        seekbar_speed.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {

            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
//                float tempo = (seekBar.getProgress() - 5000) / 100.0f;
                soundTouch.setTempoChange(seekBar.getProgressFloat());
//                playerLayout.setClickable(true);
                if (isPlaying) {
                    soundTouchRec.stopPlay();
                    soundTouchRec.startPlay(getFileToPlay());
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
//                    playerText.setText("停止");
                }
            }
        });
        seekbar_yindiao.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {

            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
//                float pitch = (seekBar.getProgress() - 1000) / 100.0f;
                soundTouch.setPitchSemi(seekBar.getProgressFloat());
//                playerLayout.setClickable(true);
//                log.setText("修改音高\n" + log.getText());
                if (isPlaying) {
                    soundTouchRec.stopPlay();
                    soundTouchRec.startPlay(getFileToPlay());
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
//                    playerText.setText("停止");
                }
            }
        });
        ll_public.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cb_public.setChecked(!cb_public.isChecked());
            }
        });
    }

    private void initView() {
        LinearLayout ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);
        tv_time = (TextView) findViewById(R.id.tv_time);
        seekbar_yindiao = (IndicatorSeekBar) findViewById(R.id.seekbar_yindiao);
        tv_yindiao_clear = (TextView) findViewById(R.id.tv_yindiao_clear);
        seekbar_speed = (IndicatorSeekBar) findViewById(R.id.seekbar_speed);
        tv_speed_clear = (TextView) findViewById(R.id.tv_speed_clear);
        iv_record_start = (ImageView) findViewById(R.id.iv_record_start);
        iv_record_cancel = (ImageView) findViewById(R.id.iv_record_cancel);
        iv_record_ok = (ImageView) findViewById(R.id.iv_record_ok);
        iv_record_stop = (ImageView) findViewById(R.id.iv_record_stop);
        iv_record_play = (ImageView) findViewById(R.id.iv_record_play);
        iv_record_pause = (ImageView) findViewById(R.id.iv_record_pause);
        iv_record_ok = (ImageView) findViewById(R.id.iv_record_ok);
        iv_record_ok = (ImageView) findViewById(R.id.iv_record_ok);
        cb_public = findViewById(R.id.cb_public);
        ll_public = findViewById(R.id.ll_public);
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_record_start://开始录音
                if (!audioRecordPermissions(RecordActivity.this)){
                    showToast("请开启录音权限");
                    break;
                }else {

                    setTime();
                    soundTouchRec.stopPlay(false);
                    //显示停止录音按钮
                    hideAll();
                    iv_record_stop.setVisibility(View.VISIBLE);

//                playerText.setText("播放");
//                    playerImage.setImageResource(R.mipmap.play);
                    isPlaying = false;

                    soundTouchRec.startRecord();
//                recorderText.setText("松开结束");
////                    recorderImage.setImageResource(R.mipmap.stop);
//                playerLayout.setClickable(false);
//                log.setText("开始录音\n" + log.getText());
                }
                break;
            case R.id.iv_record_stop://停止录音
//                recorderText.setText("按住录音");
                if (musicCurrentDuration < 3000){
                    showToast("录音太短");
                    return;
                }

                stopTime();
                hideAll();
                lastRecordFile = soundTouchRec.stopRecord();
                iv_record_play.setVisibility(View.VISIBLE);
                showOKCANCEL();
//                    recorderImage.setImageResource(R.mipmap.record);
//                playerLayout.setClickable(true);
//                log.setText("录音完成" + lastRecordFile + "\n" + log.getText());
                finishEncodeDecode = false;
                startEncodeDecode();
                break;
            case R.id.iv_record_play://开始播放

                hideAll();
                iv_record_pause.setVisibility(View.VISIBLE);
                showOKCANCEL();
                if (isPlaying) {
//                    log.setText("停止播放\n" + log.getText());
                    soundTouchRec.stopPlay();
//                    playerText.setText("播放");
//                playerImage.setImageResource(R.mipmap.play);
                } else {
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
                    soundTouchRec.startPlay(getFileToPlay());

                    //显示暂停播放按钮
                    iv_record_play.setVisibility(View.GONE);
                    iv_record_pause.setVisibility(View.VISIBLE);
//                    playerText.setText("停止");
//                playerImage.setImageResource(R.mipmap.stop);
                }
                isPlaying = true;
                break;
            case R.id.iv_record_pause://停止播放
                hideAll();
                iv_record_play.setVisibility(View.VISIBLE);
                showOKCANCEL();
                if (isPlaying) {
//                    log.setText("停止播放\n" + log.getText());
                    soundTouchRec.stopPlay();
//                    playerText.setText("播放");
//                playerImage.setImageResource(R.mipmap.play);
                } else {
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
                    soundTouchRec.startPlay(getFileToPlay());

                    //显示播放播放按钮
                    iv_record_play.setVisibility(View.VISIBLE);
                    iv_record_pause.setVisibility(View.GONE);
//                    playerText.setText("停止");
//                playerImage.setImageResource(R.mipmap.stop);
                }
                isPlaying = false;
                break;
            case R.id.tv_speed_clear:
                seekbar_speed.setProgress(0);
                soundTouch.setTempoChange(0);
//                playerLayout.setClickable(true);
                if (isPlaying) {
                    soundTouchRec.stopPlay();
                    soundTouchRec.startPlay(getFileToPlay());
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
//                    playerText.setText("停止");
                }

                break;
            case R.id.tv_yindiao_clear:
                seekbar_yindiao.setProgress(0);
                soundTouch.setPitchSemi(0);
//                playerLayout.setClickable(true);
//                log.setText("修改音高\n" + log.getText());
                if (isPlaying) {
                    soundTouchRec.stopPlay();
                    soundTouchRec.startPlay(getFileToPlay());
//                    log.setText("开始播放" + getFileToPlay() + "\n" + log.getText());
//                    playerText.setText("停止");
                }
                break;
            case R.id.iv_record_cancel:
                soundTouchRec.stopRecord();
                soundTouchRec.stopPlay(false);
                tv_time.setText("00:00");
                hideAll();
                iv_record_start.setVisibility(View.VISIBLE);
//                if (!TextUtils.isEmpty(lastRecordFile)) {
//
//                    File file = new File(getContext().getFilesDir(),lastRecordFile);
//                    if (file.exists()){
//                        file.delete();
//                    }else{
//
//                    }
//                    File file1 = new File(getContext().getFilesDir(),"tuned"+lastRecordFile);
//                    if (file1.exists()){
//                        file1.delete();
//                    }else{
//
//                    }
//                    lastRecordFile = null;
//                }
                isPlaying = false;
                break;
            case R.id.iv_record_ok:
                //保存
                hideAll();
                showOKCANCEL();
                iv_record_play.setVisibility(View.VISIBLE);
//                iv_record_ok.setVisibility(View.GONE);
//                iv_record_cancel.setVisibility(View.GONE);
//                lastRecordFile = "tuned"+lastRecordFile;不能这么设置，再次读取读不到
                if (!TextUtils.isEmpty(lastRecordFile)){
                    FileInputStream fileInputStream = null;
                    FileOutputStream outputStream = null;
                    try {
                        try {
                            //当用户未设置速度和音调时，是没有tuned开头文件生成的，所以要加判断
                            fileInputStream = getContext().openFileInput("tuned"+lastRecordFile);
                        }catch (Exception e){
                            fileInputStream = getContext().openFileInput(getFileToPlay());
                        }
                        if (fileInputStream == null){
                            fileInputStream = getContext().openFileInput(getFileToPlay());
                        }
                        String recordFile = FileUtils.getRecordFile(getThis());
                        final File file1 = new File(recordFile,lastRecordFile);

                        outputStream = new FileOutputStream(file1);

                        int len = 0;
                        byte [] bytes = new byte[1024];
                        while ((len = fileInputStream.read(bytes)) != -1){
                            outputStream.write(bytes,0,len);
                        }

                        String name = file1.getName().replace(".pcm",".wav");
                        String recordFile2 = FileUtils.getRecordFile(getThis());
                        final File file2 = new File(recordFile2,name);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {

                            startPcmToWav(file1.getAbsolutePath(),file2.getAbsolutePath());

                            }
                        }).start();

//                        RecommendFeed.DataBean.ListBean bean = new RecommendFeed.DataBean.ListBean();
//                        long currentTimeMillis = TimeFormatUtils.getCurrentTimeMillis();
//                        bean.setId(currentTimeMillis+"");
//                        bean.setTitle("我的录音");
//                        bean.setCover_name(TimeFormatUtils.longToString(currentTimeMillis,"yyyy年MM月dd日_HH时mm分ss秒")+"制作");
////                        bean.setCover_name("我的录音");
//                        bean.setUrl(file1.getAbsolutePath());
////                        saveToAli(bean);
////
//                        Intent intent = new Intent(getContext(), Add2ListActivity.class);
//                        Bundle bundle = new Bundle();
////                        bundle.putSerializable("mine_audio",bean);
//                        bundle.putSerializable("audio_id",bean);
//                        intent.putExtras(bundle);
//                        startActivity(intent);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (fileInputStream != null){
                            try {
                                fileInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (outputStream != null){
                            try {
                                outputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }else{
                    showToast("文件不存在，请重试");
                    hideAll();
                    iv_record_start.setVisibility(View.VISIBLE);
                    iv_record_ok.setVisibility(View.GONE);
                    iv_record_cancel.setVisibility(View.GONE);
                    soundTouchRec.stopRecord();
                    soundTouchRec.stopPlay(false);
                    tv_time.setText("00:00");
                }

                isPlaying = false;

                break;
        }
    }



    private void hideAll(){
        iv_record_start.setVisibility(View.GONE);
        iv_record_play.setVisibility(View.GONE);
        iv_record_pause.setVisibility(View.GONE);
        iv_record_stop.setVisibility(View.GONE);
        iv_record_ok.setVisibility(View.GONE);
        iv_record_cancel.setVisibility(View.GONE);
    }
    private void showOKCANCEL(){
        iv_record_ok.setVisibility(View.VISIBLE);
        iv_record_cancel.setVisibility(View.VISIBLE);
    }

    protected void startEncodeDecode() {
        if (needEncodeDecode && !finishEncodeDecode && lastRecordFile != null) {
            soundTouchRec.stopRecord();
//            log.setText("开始加密解密\n" + log.getText());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
//        recorderText.setText("按住录音");
//        recorderImage.setImageResource(R.mipmap.record);
//        playerLayout.setClickable(false);

//        playerText.setText("播放");
//        playerImage.setImageResource(R.mipmap.play);

        isPlaying = false;

//        soundTouchRec.stopRecord();防止锁屏后出现问题
        soundTouchRec.stopPlay(false);
//        log.setText("退出，暂停全部\n" + log.getText());
    }

    public boolean audioRecordPermissions(Activity activity){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                int RECORD_AUDIO = 1;
                activity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
                        RECORD_AUDIO);
            }
            return false;
        }
        return true;
    }

    private void stopTime(){
        if (timerTask != null){
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null){
            timer.cancel();
            timer.purge();
            timer = null;
        }
    }

    private Timer timer;
    private void setTime(){
        timer = new Timer(true);
        timerTask = new MyTimerTask();
        musicCurrentDuration = 0;
        timer.schedule(timerTask,0,1000);
    }
    private long musicCurrentDuration = 0;
    private MyTimerTask timerTask;
    private class MyTimerTask extends TimerTask {
        @Override
        public void run() {
//            int duration = mVideoView.getDuration();
            musicCurrentDuration += 1000;


//            int currentPosition = mVideoView.getCurrentPosition();
            try {
                final String s = TimeFormatUtils.longToString(musicCurrentDuration, "mm:ss");
//                final String ss = TimeFormatUtils.longToString(musicDuration,"mm:ss");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_time.setText(s);
                        if (musicCurrentDuration > 59000){
                            stopTime();
                            hideAll();
                            lastRecordFile = soundTouchRec.stopRecord();
                            iv_record_play.setVisibility(View.VISIBLE);
                            showOKCANCEL();
//                    recorderImage.setImageResource(R.mipmap.record);
//                playerLayout.setClickable(true);
//                log.setText("录音完成" + lastRecordFile + "\n" + log.getText());
                            finishEncodeDecode = false;
                            startEncodeDecode();
                        }
                    }
                });
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }

    protected String getFileToPlay() {
        return lastRecordFile;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().post(new TeaseHideFloat(true));
        if (timerTask != null){
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null){
            timer.cancel();
            timer.purge();
            timer = null;
        }
//        if (!TextUtils.isEmpty(lastRecordFile)) {
//
//            File file = new File(getContext().getFilesDir(),lastRecordFile);
//            if (file.exists()){
//                file.delete();
//            }else{
//
//            }
//            File file1 = new File(getContext().getFilesDir(),"tuned"+lastRecordFile);
//            if (file1.exists()){
//                file1.delete();
//            }else{
//
//            }
//            lastRecordFile = null;
//        }
        FileUtils.deleteFolderFile(getFilesDir().getAbsolutePath(),false);
    }

    private ProgressDialog pDialog;
    private void startDialog() {
        if (pDialog != null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pDialog == null) {
                    pDialog = new ProgressDialog(getContext());
                    pDialog.setTitle("提示");
                    pDialog.setMessage("转换中...");
                    pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    pDialog.setIndeterminate(false);
                    pDialog.setCancelable(true);
                }
                pDialog.show();
            }
        });



    }

    private void startPcmToWav(String src, String target) {
        startDialog();
        File file = new File(src);
        if (file != null) {
            try {
                @SuppressWarnings("resource")
                FileInputStream inputStream = new FileInputStream(file);
                // 计算长度
                byte[] buf = new byte[1024 * 100];
                int size = inputStream.read(buf);
                int pcmSize = 0;
                while (size != -1) {
                    pcmSize += size;
                    size = inputStream.read(buf);
                }
                inputStream.close();
                // 填入参数，比特率等。16位双声道，8000HZ
                WaveHeader header = new WaveHeader();
                // 长度字段 = 内容的大小（PCMSize) + 头部字段的大小
                // (不包括前面4字节的标识符RIFF以及fileLength本身的4字节
                header.fileLength = pcmSize + (44 - 8);
                header.FmtHdrLeth = 16;
                header.BitsPerSample = 16;
                header.Channels = 1;
                header.FormatTag = 0x0001;
                header.SamplesPerSec = 8000;
                header.BlockAlign = (short) (header.Channels
                        * header.BitsPerSample / 8);
                header.AvgBytesPerSec = header.BlockAlign
                        * header.SamplesPerSec;
                header.DataHdrLeth = pcmSize;

                byte[] h = header.getHeader();
                assert h.length == 44; // WAV标准，头部应该是44字节

                File targetFile = new File(target);
                if (!targetFile.exists()) {
                    targetFile.createNewFile();
                }
                FileOutputStream outputStream = new FileOutputStream(targetFile);
                inputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024 * 100];
                int tardetSize = inputStream.read(buffer);
                outputStream.write(h, 0, h.length);
                while (tardetSize != -1) {
                    outputStream.write(buffer, 0, tardetSize);
                    tardetSize = inputStream.read(buffer);
                }
                outputStream.close();

                FileUtils.delete(src);
                stopProgressDialog();
                toAddList(target);
            } catch (Exception e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("音频保存失败，请重试");
                    }
                });
            }
        }

    }

    private void toAddList(final String target) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                RecommendFeed.DataBean.ListBean bean = new RecommendFeed.DataBean.ListBean();
                long currentTimeMillis = TimeFormatUtils.getCurrentTimeMillis();
                bean.setId(currentTimeMillis+"");
                bean.setTitle("我的录音");
                try {
                    bean.setCover_name(TimeFormatUtils.longToString(currentTimeMillis,"yyyy年MM月dd日HH时mm分ss秒")+"制作");
                } catch (ParseException e) {
                    e.printStackTrace();
                }
//                        bean.setCover_name("我的录音");
                bean.setUrl(target);
//                        saveToAli(bean);
//
                bean.setType("2");
                if (cb_public.isChecked()){
                    bean.setIs_open("1");
                }else{
                    bean.setIs_open("-1");
                }
                bean.setDuration((musicCurrentDuration*1000)+"");
                Intent intent = new Intent(getContext(), Add2NetListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("mine_audio",bean);
//                bundle.putSerializable("audio_id",bean);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void stopProgressDialog() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pDialog != null) {
                    pDialog.dismiss();
                    pDialog = null;
                }
            }
        });
    }
}
